### Application-specific constants

APP_NAME ?= factory_test

### Environment constants 

ARCH ?= arm
CROSS_COMPILE ?= arm-linux-gnueabihf-
CC := $(CROSS_COMPILE)gcc
AR := $(CROSS_COMPILE)ar

MD134X_PATH := ../radio/md134x
SX1301_PATH := ../radio/sx1301
SX1276_PATH := ../radio/sx1276

LIB_MD134X_PATH ?= ../radio/md134x/lib
LIB_SX1301_PATH ?= ../radio/sx1301/lib
LIB_SX1276_PATH ?= ../radio/sx1276/lib

OBJ_DIR = $(shell pwd)/obj
TARGET_PATH = $(shell pwd)/target

CFLAGS = -std=c99 -D_SVID_SOURCE -D_BSD_SOURCE -Iinc -DLINUX2 -I$(SX1276_PATH)/inc -I$(SX1301_PATH)/inc -I$(MD134X_PATH)/inc

LIBS := -lsx1276 -lrt -lpthread -lm -lcrypt -ldl -L$(LIB_SX1276_PATH) -Wl,-rpath="/usr/local/lib" -Wl,-Bsymbolic

HALF_LIBS := 
#-lsx1301 -L$(LIB_SX1301_PATH) -lmd134x -L$(LIB_MD134X_PATH)
FULL_LIBS := 
#-lmd134x -L$(LIB_MD134X_PATH) -lsx1301 -L$(LIB_SX1301_PATH)

FULL_CFLAGS := -D MD134X 

INCLUDES = $(wildcard inc/*.h)

ELI_HALF_OBJS= $(OBJ_DIR)/halfduplex/main.o $(OBJ_DIR)/halfduplex/parson.o  $(OBJ_DIR)/halfduplex/current.o $(OBJ_DIR)/halfduplex/radios_hal.o

ELI_FULL_OBJS= $(OBJ_DIR)/fullduplex/main.o $(OBJ_DIR)/fullduplex/parson.o  $(OBJ_DIR)/fullduplex/current.o $(OBJ_DIR)/halfduplex/radios_hal.o

### External constant definitions
# must get library build option to know if mpsse must be linked or not

### Constant symbols


### Constants for Lora concentrator HAL library
# List the library sub-modules that are used by the application

LGW_HALF_DUPLEX_INC =
ifneq ($(wildcard $(SX1301_PATH)/inc/config.h),)
  # only for HAL version 1.3 and beyond
  LGW_HALF_DUPLEX_INC += $(SX1301_PATH)/inc/config.h
endif
LGW_HALF_DUPLEX_INC += $(SX1301_PATH)/inc/loragw_hal.h


LGW_FULL_DUPLEX_INC =
ifneq ($(wildcard $(MD134X_PATH)/inc/config.h),)
  # only for HAL version 1.3 and beyond
  LGW_FULL_DUPLEX_INC += $(MD134X_PATH)/inc/config.h
endif
LGW_FULL_DUPLEX_INC += $(MD134X_PATH)/inc/loragw_hal.h

### Linking options


### General build targets

all:dirs eli
    
eli: eli_halfduplex
#eli_fullduplex

dirs:
	-@mkdir $(OBJ_DIR)/halfduplex -p
	-@mkdir ${TARGET_PATH}/halfduplex -p
	-@mkdir $(OBJ_DIR)/fullduplex -p 
	-@mkdir ${TARGET_PATH}/fullduplex -p 



#FACTORY_TEST
eli_halfduplex:$(ELI_HALF_OBJS)
	@echo "---halfduplex factory test--------------------------------"
	$(CC) $^ -o $(TARGET_PATH)/halfduplex/${APP_NAME} $(LIBS) 	
#$(CC) -L$(LIB_SX1301_PATH) $^ -o $(TARGET_PATH)/halfduplex/${APP_NAME} $(LIBS) $(HALF_LIBS)


eli_fullduplex:$(ELI_FULL_OBJS)
	@echo "---fullduplex factory test--------------------------------"
	$(CC) -L$(LIB_MD134X_PATH) $^ -o $(TARGET_PATH)/fullduplex/${APP_NAME} $(LIBS) $(FULL_LIBS)


clean:
	@rm $(OBJ_DIR) -rf
	@rm $(TARGET_PATH) -rf

### Sub-modules compilation


$(OBJ_DIR):
	@mkdir -p $(OBJ_DIR)/fullduplex
	@mkdir -p $(OBJ_DIR)/halfduplex

$(OBJ_DIR)/halfduplex/%.o: src/%.c $(INCLUDES) | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $< -o $@
#$(CC) -c $(CFLAGS) -I$(SX1301_PATH)/inc $< -o $@

$(OBJ_DIR)/fullduplex/%.o: src/%.c $(INCLUDES) | $(OBJ_DIR)
	$(CC) -c $(CFLAGS) $(FULL_CFLAGS) -I$(MD134X_PATH)/inc $< -o $@

### EOF

